﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CandidateManager.Areas.Coach.Models;
using WI.CandidateManager.Business.Objects;
using WI.CandidateManager.Common.Data.Entities;

namespace CandidateManager.Areas.Coach.Controllers
{
    public class ScheduleController : CandidateManager.Controllers.AutorizeBaseController
    {
        private ScheduleBO scheduleBO;

        public ScheduleController()
            : base()
        {
            scheduleBO = new ScheduleBO();
        }

        [Authorize]
        public ActionResult List()
        {
            ScheduleModifyModel scheduleModify = new ScheduleModifyModel()
            {
                Subjects = new SubjectBO().GetSubjects(),
                Groups = new GroupBO().GetStudyGroups(),
                Schedules = scheduleBO.GetSchedules(),
            };

            return View(scheduleModify);

        }

        [Authorize]
        public ActionResult Delete(long subjectId, long groupId)
        {
            if (scheduleBO.DeleteSchedule(subjectId, groupId))
            {
                return RedirectToAction("List", "Schedule");
            }
            else
            {
                return RedirectToAction("List", "Schedule");
            }
        }

        [Authorize]
        public ActionResult Edit(long subjectId, long groupId)
        {
            var scheduleModify = new ScheduleModifyModel()
            {
                Subjects = new SubjectBO().GetSubjects(),
                Groups = new GroupBO().GetStudyGroups(),
                GroupId = groupId,
                SubjectId = subjectId,
                Schedule = scheduleBO.GetSchedules(subjectId, groupId).Single()
            };

            return View("Create", scheduleModify);
        }

        [Authorize]
        [HttpPost]
        public ActionResult Edit(ScheduleModifyModel model)
        {
            model.Schedule.StudyGroupId = model.GroupId;
            model.Schedule.SubjectId = model.SubjectId;
            if (scheduleBO.Update(model.Schedule))
            {
                return RedirectToAction("List");
            }
            else
            {
                return RedirectToAction("List");
            }
        }

        [Authorize]
        public ActionResult Create()
        {
            var scheduleModify = new ScheduleModifyModel()
                {
                    Schedule = new Schedule(),
                    Subjects = new SubjectBO().GetSubjects(),
                    Groups = new GroupBO().GetStudyGroups()
                };

            return View(scheduleModify);
        }

        [Authorize]
        [HttpPost]
        public ActionResult Create(ScheduleModifyModel model)
        {
            model.Schedule.StudyGroupId = model.GroupId;
            model.Schedule.SubjectId = model.SubjectId;
            if (scheduleBO.CreateSchedule(model.Schedule))
            {
                return RedirectToAction("List", "Schedule");
            }
            else
            {
                return RedirectToAction("Create", "Schedule");
            }
        }
    }
}
